package com.cai.primeton.controller;

import com.cai.primeton.entity.PBook;
import com.cai.primeton.entity.PType;
import com.cai.primeton.entity.R;
import com.cai.primeton.repository.BookRepo;
import com.cai.primeton.repository.BorrowRepo;
import com.cai.primeton.repository.CategoryRepo;
import com.cai.primeton.service.BookService;
import com.cai.primeton.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @version 1.0
 * @author：蔡松坡
 * @date：2021/7/1 16 : 09
 * @package：com.cai.primeton.controller
 */
@Controller
@Api(tags = "图书分类管理")
public class CategoryController {

//    @Autowired
//    private CategoryRepo categoryRepo;

    @Autowired
    private CategoryService categoryService;

//    @Autowired
//    private BookRepo bookRepo;

    @Autowired
    private BookService bookService;

    @GetMapping("/category/list")
    public String category(Model model){
//        List<PType> all = categoryRepo.findAll();
        List<PType> all = categoryService.findAll();
        model.addAttribute("types",all);
        return "category/categorylist";
    }



    @ApiOperation(value = "查找图书分类信息")
    @GetMapping("/category/findAll")
    public List<PType> findALlType(){
//        List<PType> all = categoryRepo.findAll();
        List<PType> all = categoryService.findAll();
        return all;
    }

    /**
     * 录入分类信息
     * @param pType
     * @return
     */
    @ApiOperation(value = "录入分类信息")
//    @ApiImplicitParam(name = "pType",value = "需要传入分类信息")
    @PostMapping("/category/insert")
    @ResponseBody
    public Map insert(PType pType){
//        categoryRepo.save(pType);
        categoryService.save(pType);
        R r = new R(200,"success");
        return r.result();
    }

    /**
     * 根据id 查询图书信息
     * @param id
     * @return
     */
    @ApiOperation(value = "查询类型信息")
    @ApiImplicitParam(name = "id",value = "要查询的图书的编号")
    @GetMapping("/category/find/{id}")
    public PType findById(@PathVariable("id") Integer id){
//        Optional<PType> byId = categoryRepo.findById(id);
        PType byId = categoryService.findById(id);
        return byId;
    }

    /**
     * 根据id 修改类型信息
     * @param pType
     * @return
     */
    @ApiOperation(value = "修改类型信息")
//    @ApiImplicitParam(name = "PType",value = "传入的要修改的图书信息，其中图书编号为必填项")
    @PutMapping("/category/update")
    @ResponseBody
    public Map updateCate(PType pType){
        if(pType.getId()==null){
            R r = new R(333,"类型编号不能为空");
            return r.result();
        }
//        categoryRepo.save(pType);
        categoryService.save(pType);
        R r = new R(200,"success");
        return r.result();
    }

    /**
     * 根据id 删除图书信息
     * @param id
     * @return
     */
    @ApiOperation(value = "删除图书信息")
    @ApiImplicitParam(name = "id",value = "要删除图书的id")
    @DeleteMapping("/category/delete/{id}")
    @ResponseBody
    public Map deleteCate(@PathVariable("id") Integer id){
//        categoryRepo.deleteById(id);
        categoryService.deleteById(id);
        R r = new R(200,"success");
        return r.result();
    }

    /**
     * 根据类型 查询所有图书信息
     * @param type
     * @return
     */
    @ApiOperation(value = "根据类型查询所有图书信息")
    @ApiImplicitParam(name = "type",value = "传入图书的类型")
    @GetMapping("/category/findBook/{type}")
    @ResponseBody
    public List findBooks(@PathVariable("type") Integer type){
//        List<PBook> aLlBooks = bookRepo.findAllByType(type);
        List<PBook> allByType = bookService.findAllByType(type);
        return allByType;
    }

    @ApiOperation(value = "查询图书类型",notes = "根据图书的名称查询图书类型")
    @ApiImplicitParam(name = "name",value = "图书名称")
    @GetMapping("/category/findType/{name}")
    @ResponseBody
    public String findType(@PathVariable("name") String name){
//        String str = categoryRepo.findType(name);
        String type = categoryService.findType(name);
        return type;
    }


}
